package com.eystar.config;

import com.eystar.ext.aop.ExceptionInterceptor;
import com.eystar.ext.aop.PathInterceptor;
import com.eystar.ext.engine.MyLongExt;
import com.eystar.ext.plugin.tablebind.AutoTableBindPlugin;
import com.eystar.ext.plugin.tablebind.ModelExt;
import com.eystar.ext.route.AutoBindRoutes;
import com.eystar.pdc.aop.auth.AuthInterceptor;
import com.eystar.pdc.aop.log.LogInterceptor;
import com.eystar.pdc.aop.log.ThreadLog;
import com.eystar.pdc.controller.BaseController;
import com.eystar.pdc.directive.AuthHasDirective;
import com.eystar.pdc.directive.AuthJsonDirective;
import com.eystar.pdc.directive.DicForDirective;
import com.eystar.pdc.directive.DicItemGetDirective;
import com.eystar.pdc.directive.DicJsonDirective;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;
import com.jfinal.template.source.ClassPathSourceFactory;

public class BaseConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		PropKit.use("little_config.txt");
		me.setDevMode(true);
		me.setError500View("/common/500.html");
		me.setError500View("/common/404.html");
	}

	@SuppressWarnings("unchecked")
	@Override
	public void configRoute(Routes me) {
		AutoBindRoutes abr = new AutoBindRoutes();
		abr.setBaseViewPath("/WEB-INF/templates");
		abr.addExcludeClasses(BaseController.class);
		me.add(abr);
	}

	@Override
	public void configEngine(Engine me) {
		// 添加指令
		me.addDirective("dic_for", DicForDirective.class);
		me.addDirective("dic_json", DicJsonDirective.class);
		me.addDirective("dic_item_get", DicItemGetDirective.class);
		me.addDirective("auth_json", AuthJsonDirective.class);
		me.addDirective("auth_has", AuthHasDirective.class);
		me.setDevMode(true);
		Engine.addExtensionMethod(Long.class, MyLongExt.class);
	}

	@SuppressWarnings("unchecked")
	@Override
	public void configPlugin(Plugins me) {
		DruidPlugin druidPlugin = new DruidPlugin(
				PropKit.get("jdbcUrl"),PropKit.get("user"), 
				PropKit.get("password"),PropKit.get("driver"));
		
		AutoTableBindPlugin autoTableBindPlugin = new AutoTableBindPlugin(druidPlugin);
		autoTableBindPlugin.addExcludeClasses(ModelExt.class);
		autoTableBindPlugin.setDevMode(true);
		autoTableBindPlugin.setShowSql(true);
		autoTableBindPlugin.getEngine().setSourceFactory(new ClassPathSourceFactory());
		autoTableBindPlugin.addSqlTemplate("all.sql");
		me.add(druidPlugin);
		me.add(autoTableBindPlugin);
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new SessionInViewInterceptor());
		me.add(new PathInterceptor());
		me.add(new ExceptionInterceptor("/common/500.html"));
		me.add(new AuthInterceptor());
		me.add(new LogInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		
	}

	public void afterJFinalStart(){
		// 开启日志启动线程
		ThreadLog.startSaveDBThread();
	}
}
